//
//  JRBaseAlertView.swift
//
//
//  Created by rui on 2024/4/19.
//

import UIKit
import Foundation

private let globalInstancesLock: dispatch_semaphore_t = DispatchSemaphore(value: 1)

private let JR_QUEUE_ALERTVIEW_KEY = "djr.alertview.operation.queue"

open class JRBaseAlertView: JRBaseView {

    open lazy var containView: JRBaseView = {
        let v = JRBaseView(frame: CGRectZero)
        v.backgroundColor = .white
        self.addSubview(v)
        return v
    }()
    
    open override func customInit() {
        super.customInit()
        self.backgroundColor = .black.withAlphaComponent(0.6)
        self.frame = UIScreen.main.bounds
    }
    
    open var isTouchDismiss = true
    
    open var isTouchDismissAnimated = true
    
    open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touchPoint = touches.randomElement()?.location(in: self) else { return }
        let responderView = self.hitTest(touchPoint, with: event)
        if responderView != nil, responderView != self.containView, self.isTouchDismiss {
            self.dismiss(self.isTouchDismissAnimated)
        }
    }
    
    open func show(_ animated: Bool = true, withDuration duration: TimeInterval = 0.35, completion closure: (()->Void)? = nil) {
        DispatchQueue(label: JR_QUEUE_ALERTVIEW_KEY, qos: .default).async {
            globalInstancesLock.wait()
            DispatchQueue.main.async {
                guard let window = UIWindow.getKeyWindow() else {
                    return
                }
                window.addSubview(self)
                self.alpha = 1.0;
                let transform = CGAffineTransformScale(.identity, 1.0, 1.0)
                self.containView.transform = CGAffineTransformScale(.identity, 0.2, 0.2)
                self.containView.alpha = 0.0
                if animated {
                    UIView.animate(withDuration: duration, delay: 0.1, usingSpringWithDamping: 0.75, initialSpringVelocity: 7) {
                        self.backgroundColor = .black.withAlphaComponent(0.4)
                        self.containView.transform = transform
                        self.containView.alpha = 1.0
                        closure?()
                    }
                } else {
                    self.backgroundColor = .black.withAlphaComponent(0.4)
                    self.containView.transform = transform
                    self.containView.alpha = 1.0
                    closure?()
                }

            }
        }
    }
    
    open func dismiss(_ animated: Bool = true, withDuration duration: TimeInterval = 0.35, completion closure: (()->Void)? = nil) {
        DispatchQueue(label: JR_QUEUE_ALERTVIEW_KEY, qos: .default).async {
            globalInstancesLock.signal()
            DispatchQueue.main.async {
                if animated {
                    UIView.animate(withDuration: duration) {
                        self.alpha = 0.1
                    } completion: { finished in
                        closure?()
                        self.removeFromSuperview()
                    }
                } else {
                    closure?()
                    self.removeFromSuperview()
                }
            }
        }
    }
}

extension JRBaseAlertView {
    
}
